package com.farm.learn.service.impl;

import com.farm.learn.domain.UserHour;
import com.farm.core.time.TimeTool;
import org.apache.log4j.Logger;
import com.farm.learn.dao.UserhourDaoInter;
import com.farm.learn.service.UserhourServiceInter;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.query.DataQuerys;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import com.farm.core.auth.domain.LoginUser;

/* *
 *功能：用户课时服务层实现类
 *详细：
 *
 *版本：v0.1
 *作者：FarmCode代码工程
 *日期：20150707114057
 *说明：
 */
@Service
public class UserhourServiceImpl implements UserhourServiceInter {
	@Resource
	private UserhourDaoInter userhourDaoImpl;

	private static final Logger log = Logger.getLogger(UserhourServiceImpl.class);

	@Override
	@Transactional
	public UserHour insertUserhourEntity(UserHour entity, LoginUser user) {
		// TODO 自动生成代码,修改后请去除本注释
		// entity.setCuser(user.getId());
		// entity.setCtime(TimeTool.getTimeDate14());
		// entity.setCusername(user.getName());
		// entity.setEuser(user.getId());
		// entity.setEusername(user.getName());
		// entity.setEtime(TimeTool.getTimeDate14());
		// entity.setPstate("1");
		return userhourDaoImpl.insertEntity(entity);
	}

	@Override
	@Transactional
	public UserHour editUserhourEntity(UserHour entity, LoginUser user) {
		// TODO 自动生成代码,修改后请去除本注释
		UserHour entity2 = userhourDaoImpl.getEntity(entity.getId());
		// entity2.setEuser(user.getId());
		// entity2.setEusername(user.getName());
		// entity2.setEtime(TimeTool.getTimeDate14());
		entity2.setEtime(entity.getEtime());
		entity2.setStime(entity.getStime());
		entity2.setClassid(entity.getClassid());
		entity2.setUserid(entity.getUserid());
		entity2.setPstate(entity.getPstate());
		entity2.setHourid(entity.getHourid());
		entity2.setId(entity.getId());
		userhourDaoImpl.editEntity(entity2);
		return entity2;
	}

	@Override
	@Transactional
	public void deleteUserhourEntity(String id, LoginUser user) {
		// TODO 自动生成代码,修改后请去除本注释
		userhourDaoImpl.deleteEntity(userhourDaoImpl.getEntity(id));
	}

	@Override
	@Transactional
	public UserHour getUserhourEntity(String id) {
		// TODO 自动生成代码,修改后请去除本注释
		if (id == null) {
			return null;
		}
		return userhourDaoImpl.getEntity(id);
	}

	@Override
	@Transactional
	public DataQuery createUserhourSimpleQuery(DataQuery query) {
		DataQuery dbQuery = DataQuery.init(query,
				"WLP_U_USERHOUR H LEFT JOIN ALONE_AUTH_USERORG C ON C.USERID = H.USERID LEFT JOIN ALONE_AUTH_ORGANIZATION D ON D.ID = C.ORGANIZATIONID",
				"H.ID AS ID,H.PSTATE AS LHOURSTATE, H.STIME AS STIME, H.ETIME AS ETIME, H.LTIME AS LTIME, H.PSTATE AS PSTATE, D. NAME AS ORGNAME");

		return dbQuery;
	}

	@Override
	@Transactional
	public void initMoreUserHourInfos(List<Map<String, Object>> resultList) throws SQLException {
		List<String> ids = new ArrayList<String>();
		for (Map<String, Object> node : resultList) {
			ids.add((String) node.get("ID"));
		}
		DataQuery dbQuery = DataQuery.getInstance(1,
				"A.ID as ID,B. NAME AS USERNAME, B.LOGINNAME AS LOGINNAME, C.PROCESS AS PROCESS, D.`NAME` AS CLASSNAME, E.TITLE AS HOURNAME, F.DURATION AS DURATION, F.PTIME AS PTIME",
				"WLP_U_USERHOUR A LEFT JOIN ALONE_AUTH_USER B ON B.ID = A.USERID LEFT JOIN WLP_U_USERCLASS C ON C.CLASSID = A.CLASSID AND C.USERID = A.USERID LEFT JOIN WLP_L_CLASS D ON D.ID = A.CLASSID LEFT JOIN WLP_L_CLASSHOUR E ON E.ID = A.HOURID LEFT JOIN WLP_U_PLAYTIME F ON F.HOURID = A.HOURID AND F.USERKEY = A.USERID");
		dbQuery.addSqlRule(" AND a.ID IN (" + DataQuerys.getWhereInSubVals(ids) + ")");
		dbQuery.setNoCount();
		dbQuery.setPagesize(10000);
		List<Map<String, Object>> infos = dbQuery.search().getResultList();
		Map<String, Map<String, Object>> infoDatas = new HashMap<String, Map<String, Object>>();
		for (Map<String, Object> node : infos) {
			infoDatas.put((String) node.get("ID"), node);
		}
		for (Map<String, Object> node : resultList) {
			node.putAll(infoDatas.get((String) node.get("ID")));
		}
	}

}
